package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dao.AdminDao;
import com.pojo.AdminPojo;
import com.util.Log;


public class EditAdminInfo extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		String name = request.getParameter("name");
		String pwd = request.getParameter("pwd");
		String adminId = request.getParameter("adminId");
		int id = 0;
		if(!"".equals(adminId)) id = Integer.parseInt(adminId);
		String message = "";
		if (name.equals("")){
			message = "请输入账号！";
		}else if (pwd.equals("")){
			message = "请输入密码！";
		}  else {
			message = "保存失败!";
			AdminPojo admin = new AdminPojo();
			admin.setName(name);
			admin.setPwd(pwd);
			admin.setAdminId(id);
			AdminDao dao = new AdminDao();
			int a = dao.editAdmin(admin);
			dao.closeAll();
			if(a==0) message="保存成功";
			else if(a==1) message="存在相同的管理员账号";
		}
		String json = "{\"status\":\""+message+"\"}";
		Log.write(json);
		out.println(json);
		out.flush();
        out.close();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
